#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/wait.h>

int main()
{
  int pipe_fd[2]={0};
  int n = pipe(pipe_fd);
  if(n!=0)
  {
    perror("pipe fail\n");
    return 1;
  }
  int pid =fork();
  if(pid<0)
  {
    perror("fork fail\n");
    return 1;
  }
  else if(pid==0)
  {
    close(pipe_fd[0]);
    const char* buffer="hello world";
    int count=0;
    while(1)
    {
     // count++;
     // write(pipe_fd[1],"a",1);
     // printf("count:%d\n",count);
      sleep(1);
      write(pipe_fd[1],buffer,strlen(buffer));
     break;
    }
    close(pipe_fd[1]);
  }

  close(pipe_fd[1]);
  while(1)
  {
    //sleep(5);
    char buff[1024*2] = {0};
    ssize_t s = read(pipe_fd[0],buff,sizeof(buff)+1);
    if(s>0)
    {
      buff[s]=0;
      printf("child message#  %c\n",buff[0]);
    }
    else if(s==0)
    {
      printf("pipe file close, child quit!\n");
      break;
    }
    else{
      break;
    }
   // break;
  }
  //close(pipe_fd[0]);
  int status = 0;
  n = waitpid(-1,&status,0);
  printf("status code:%d,status signal:%d\n",(status>>8)&0xff,status&0x7f);
  //printf("%d,%d\n",pipe_fd[0],pipe_fd[1]);
  return 0;
}
